TARGET=libasio_tcp_sock.a

FILES= \
	logger.cpp \
	extra_timer.cpp \
	client_socket.cpp \
	server_socket.cpp \
	connection_manager.cpp \
	pkg_parser.cpp \
	connection.cpp \
	callback_handler.cpp \
	io_service_pool.cpp

VPATH=..
OBJEXT = o
DEPEXT = d
OBJPATH = .obj
DEPPATH = .dep

OBJS=$(FILES:%.cpp=$(OBJPATH)/%.$(OBJEXT))
DEPS=$(FILES:%.cpp=$(DEPPATH)/%.$(DEPEXT))

CXX=g++
STRIP=strip
AR=ar

DEPFLAGS = -MM -MF"$(@:%.$(OBJEXT)=%.$(DEPEXT))" -MT"$(@:$(DEPPATH)/%.$(DEPEXT)=$(OBJPATH)/%.$(OBJEXT))"

CPPFLAGS += -Wall -I. -pthread
#CPPFLAGS += -fmessage-length=0

OS=$(shell uname -o)
ifeq ($(OS),Msys)
	CPPFLAGS += -fexec-charset=GBK -finput-charset=UTF-8
endif

LIB_ASIO = -lboost_system -lboost_thread
ifeq ($(OS),Msys)
	LIB_ASIO = -lboost_system-mt -lboost_thread-mt -lwsock32 -lws2_32
endif

INSPREFIX = /usr
ifeq ($(OS),Msys)
INSPREFIX = $(MINGW_PREFIX)
endif

LDFLAGS = -lpthread -L. -lasio_tcp_sock
LDFLAGS+= $(LIB_ASIO) 

all: $(TARGET)

$(TARGET):$(OBJS)
#	$(CXX) -o $@ $^ $(LDFLAGS)
#	cp -f $@ $@_g
#	$(STRIP) -sg $@
	$(AR) cr $@ $^

$(DEPS):$(DEPPATH)/%.$(DEPEXT):%.cpp
	@test -d `dirname $@`||mkdir -p `dirname $@` 2>/dev/null || true
	$(CXX) $< $(CPPFLAGS) $(DEPFLAGS)

-include $(DEPS)

$(OBJS):$(OBJPATH)/%.$(OBJEXT):%.cpp
	@test -d `dirname $@`||mkdir -p `dirname $@` 2>/dev/null || true
	$(CXX) -O0 -ggdb -c $< -o $@ $(CPPFLAGS)

test-clean:
	rm -rf test-server test-client

clean: test-clean
	rm -rf $(OBJPATH) $(TARGET) *.pc
	
test: test-server test-client

test-server:$(TARGET) test-server.cpp
	$(CXX) -O0 -ggdb test-server.cpp -o $@ $(CPPFLAGS) $(LDFLAGS)
test-client:$(TARGET) test-client.cpp 
	$(CXX) -O0 -ggdb test-client.cpp -o $@ $(CPPFLAGS) $(LDFLAGS)
	
install: $(TARGET) asio_tcp_sock.pc
	install -d $(INSPREFIX)/include/asio_tcp_sock
	install -m 644 *.h $(INSPREFIX)/include/asio_tcp_sock
	install -m 644 buf_stream.hpp $(INSPREFIX)/include/asio_tcp_sock
	install -m 644 $< $(INSPREFIX)/lib
	install -m 644 asio_tcp_sock.pc $(INSPREFIX)/lib/pkgconfig
uninstall:
	rm -rf $(INSPREFIX)/include/asio_tcp_sock $(INSPREFIX)/lib/$(TARGET) $(INSPREFIX)/lib/pkgconfig/asio_tcp_sock.pc
asio_tcp_sock.pc:
	echo 'prefix='$(INSPREFIX) > $@
	echo 'exec_prefix=$(INSPREFIX)' >> $@
	echo 'libdir=$(INSPREFIX)/lib' >> $@
	echo 'includedir=$(INSPREFIX)/include/asio_tcp_sock' >> $@
	echo '' >> $@
	echo 'Name: asio_tcp_sock' >> $@
	echo 'Description: asio_tcp_sock is base on Boost.Asio network library' >> $@
	echo 'Version: 0.1' >> $@
	echo 'Libs: -lpthread -lasio_tcp_sock $(LIB_ASIO)' >> $@
	echo 'Cflags: -I$${includedir} -pthread' >> $@
 
.PHONY: clean test test-clean
